package com.xm.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xm.pojo.Goods;
import com.xm.pojo.Store;
import com.xm.pojo.StoreGoods;
import com.xm.pojo.User;
import com.xm.service.StoreService;
import com.xm.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController//返回json类型的数据
@RequestMapping("store")//一级访问路径
public class StoreController {
    @Autowired //自动装配，将spring容器中的PatientService类型对象，赋值给patientService
    private StoreService storeService;

    @RequestMapping("findByPage")//分页显示接口
    public Map findByPage(@RequestParam(defaultValue = "") String v,
                          @RequestParam(defaultValue = "") String c,
                          @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "limit", required = false) Integer pageSize) {
        List<Store> list = null;
        //System.out.println(v+"--------------------------------------------------------------");
        //System.out.println(c+"--------------------------------------------------------------");
        if (v.equals("")) {
            PageHelper.startPage(pageNum, pageSize);
            list = storeService.findAll();
            PageInfo<Object> pageInfo = new PageInfo<Object>(list);
            HashMap<String, Object> map = new HashMap<>();
            map.put("code", 0);
            map.put("msg", "success");
            map.put("count", pageInfo.getTotal());//总记录数
            map.put("data", pageInfo.getList());//分页数据
            return map;
        }
        if (!v.equals("")) {
            if (c.equals("1")) {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findByNum(v);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
            if (c.equals("2")) {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findByStore(v);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
            if (c.equals("3")) {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findByName(v);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
            if (c.equals("4")) {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findByType(v);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            } else {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findAll();
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
        } else {
            PageHelper.startPage(pageNum, pageSize);
            list = storeService.findAll();
            PageInfo<Object> pageInfo = new PageInfo<Object>(list);
            HashMap<String, Object> map = new HashMap<>();
            map.put("code", 0);
            map.put("msg", "success");
            map.put("count", pageInfo.getTotal());//总记录数
            map.put("data", pageInfo.getList());//分页数据
            return map;
        }
    }

    @RequestMapping("findByPage2")//分页显示接口
    public Map findByPage2(@RequestParam(defaultValue = "") String v,
                           @RequestParam(defaultValue = "") String c,
                           @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "limit", required = false) Integer pageSize) {
        List<Store> list = null;
        //System.out.println(v+"--------------------------------------------------------------");
        //System.out.println(c+"--------------------------------------------------------------");
        if (v.equals("")) {
            PageHelper.startPage(pageNum, pageSize);
            list = storeService.findAll2();
            PageInfo<Object> pageInfo = new PageInfo<Object>(list);
            HashMap<String, Object> map = new HashMap<>();
            map.put("code", 0);
            map.put("msg", "success");
            map.put("count", pageInfo.getTotal());//总记录数
            map.put("data", pageInfo.getList());//分页数据
            return map;
        }
        if (!v.equals("")) {
            if (c.equals("1")) {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findByNum2(v);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
            if (c.equals("2")) {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findByStore2(v);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
            if (c.equals("3")) {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findByName2(v);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
            if (c.equals("4")) {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findByType2(v);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            } else {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findAll2();
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
        } else {
            PageHelper.startPage(pageNum, pageSize);
            list = storeService.findAll2();
            PageInfo<Object> pageInfo = new PageInfo<Object>(list);
            HashMap<String, Object> map = new HashMap<>();
            map.put("code", 0);
            map.put("msg", "success");
            map.put("count", pageInfo.getTotal());//总记录数
            map.put("data", pageInfo.getList());//分页数据
            return map;
        }
    }

    @RequestMapping("findByPage3")//分页显示接口
    public Map findByPage3(@RequestParam(defaultValue = "") String v,
                           @RequestParam(defaultValue = "") String c,
                           @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "limit", required = false) Integer pageSize) {
        List<Store> list = null;
        //System.out.println(v+"--------------------------------------------------------------");
        //System.out.println(c+"--------------------------------------------------------------");
        if (v.equals("")) {
            PageHelper.startPage(pageNum, pageSize);
            list = storeService.findAll3();
            PageInfo<Object> pageInfo = new PageInfo<Object>(list);
            HashMap<String, Object> map = new HashMap<>();
            map.put("code", 0);
            map.put("msg", "success");
            map.put("count", pageInfo.getTotal());//总记录数
            map.put("data", pageInfo.getList());//分页数据
            return map;
        }
        if (!v.equals("")) {
            if (c.equals("1")) {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findByNum3(v);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
            if (c.equals("2")) {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findByStore3(v);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
            if (c.equals("3")) {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findByName3(v);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
            if (c.equals("4")) {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findByType3(v);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            } else {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findAll3();
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
        } else {
            PageHelper.startPage(pageNum, pageSize);
            list = storeService.findAll3();
            PageInfo<Object> pageInfo = new PageInfo<Object>(list);
            HashMap<String, Object> map = new HashMap<>();
            map.put("code", 0);
            map.put("msg", "success");
            map.put("count", pageInfo.getTotal());//总记录数
            map.put("data", pageInfo.getList());//分页数据
            return map;
        }
    }

    @RequestMapping("findByPage4")//分页显示接口
    public Map findByPage4(@RequestParam(defaultValue = "") String v,
                           @RequestParam(defaultValue = "") String c,
                           @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "limit", required = false) Integer pageSize) {
        List<Store> list = null;
        //System.out.println(v+"--------------------------------------------------------------");
        //System.out.println(c+"--------------------------------------------------------------");
        if (v.equals("")) {
            PageHelper.startPage(pageNum, pageSize);
            list = storeService.findAll4();
            PageInfo<Object> pageInfo = new PageInfo<Object>(list);
            HashMap<String, Object> map = new HashMap<>();
            map.put("code", 0);
            map.put("msg", "success");
            map.put("count", pageInfo.getTotal());//总记录数
            map.put("data", pageInfo.getList());//分页数据
            return map;
        }
        if (!v.equals("")) {
            if (c.equals("1")) {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findByNum4(v);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
            if (c.equals("2")) {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findByStore4(v);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
            if (c.equals("3")) {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findByName4(v);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
            if (c.equals("4")) {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findByType4(v);
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            } else {
                PageHelper.startPage(pageNum, pageSize);
                list = storeService.findAll4();
                PageInfo<Object> pageInfo = new PageInfo<Object>(list);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", pageInfo.getTotal());//总记录数
                map.put("data", pageInfo.getList());//分页数据
                return map;
            }
        } else {
            PageHelper.startPage(pageNum, pageSize);
            list = storeService.findAll4();
            PageInfo<Object> pageInfo = new PageInfo<Object>(list);
            HashMap<String, Object> map = new HashMap<>();
            map.put("code", 0);
            map.put("msg", "success");
            map.put("count", pageInfo.getTotal());//总记录数
            map.put("data", pageInfo.getList());//分页数据
            return map;
        }
    }

    @RequestMapping("yes")//通过
    public String yes(Store store) {
        System.out.println(store);
        storeService.yes(store);
        return "success";
    }

    @RequestMapping("no")//通过
    public String no(Store store) {
        //System.out.println(medical);
        storeService.no(store);
        return "success";
    }

    @RequestMapping("yes2")//通过
    public String yes2(Store store) {
        System.out.println(store);
        storeService.yes2(store);
        return "success";
    }

    @RequestMapping("no2")//通过
    public String no2(Store store) {
        //System.out.println(medical);
        storeService.no2(store);
        return "success";
    }

    @RequestMapping("no3")//通过
    public String no3(Store store) {
        //System.out.println(medical);
        storeService.no3(store);
        return "success";
    }

    @RequestMapping("yes4")//通过
    public String yes4(Store store) {
        // System.out.println(store);
        storeService.yes4(store);
        return "success";
    }

    @RequestMapping("no4")//通过
    public String no4(Store store) {
        //System.out.println(medical);
        storeService.no4(store);
        return "success";
    }

    @RequestMapping("wirteExcel")//导出接口
    public void wirteExcel(HttpServletResponse response) throws IOException {
        //System.out.println(Arrays.toString(ids));
        String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        List<Store> storeList = storeService.findAll();
        String fileName = "store-" + time + ".xlsx";
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        ServletOutputStream outputStream = response.getOutputStream();
        EasyExcel.write(outputStream, Store.class).sheet("待审核商家列表").doWrite(storeList);
    }

    @RequestMapping("wirteExcel2")//导出接口
    public void wirteExcel2(HttpServletResponse response) throws IOException {
        //System.out.println(Arrays.toString(ids));
        String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        List<Store> storeList = storeService.findAll2();
        String fileName = "store-" + time + ".xlsx";
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        ServletOutputStream outputStream = response.getOutputStream();
        EasyExcel.write(outputStream, Store.class).sheet("待缴费商家列表").doWrite(storeList);
    }

    @RequestMapping("wirteExcel3")//导出接口
    public void wirteExcel3(HttpServletResponse response) throws IOException {
        //System.out.println(Arrays.toString(ids));
        String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        List<Store> storeList = storeService.findAll3();
        String fileName = "store-" + time + ".xlsx";
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        ServletOutputStream outputStream = response.getOutputStream();
        EasyExcel.write(outputStream, Store.class).sheet("营业中商家列表").doWrite(storeList);
    }

    @RequestMapping("wirteExcel4")//导出接口
    public void wirteExcel4(HttpServletResponse response) throws IOException {
        //System.out.println(Arrays.toString(ids));
        String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        List<Store> storeList = storeService.findAll4();
        String fileName = "store-" + time + ".xlsx";
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        ServletOutputStream outputStream = response.getOutputStream();
        EasyExcel.write(outputStream, Store.class).sheet("整改中商家列表").doWrite(storeList);
    }

    @RequestMapping("updateStore")//编辑接口
    public String updateStore(Store store) {
        storeService.updateStore(store);
        return "success";
    }
    @RequestMapping("getStoreGoods")//分页显示接口
    public Map getStoreGoods(String num,
                             @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "limit", required = false) Integer pageSize) {
        System.out.println(num+"-------------------------------------------------------");
        List<StoreGoods> list = null;
        PageHelper.startPage(pageNum, pageSize);
        list = storeService.getStoreGoods(num);
        PageInfo<Object> pageInfo = new PageInfo<Object>(list);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", pageInfo.getTotal());//总记录数
        map.put("data", pageInfo.getList());//分页数据
        return map;
    }

}
